package com.collect.zambia.zhiUtils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.text.TextUtils;

import androidx.core.app.ActivityCompat;

import java.util.ArrayList;
import java.util.List;

public class PhoneAccountUtil {

    public static ArrayList<String> getAllPhoneAccountHandles(Context context) {
        TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return null;
        }
        List<PhoneAccountHandle> phoneAccountHandles = telecomManager.getCallCapablePhoneAccounts();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < phoneAccountHandles.size(); i++) {
            PhoneAccountHandle phoneAccountHandle = phoneAccountHandles.get(i);
            String accountId = phoneAccountHandle.getId();
            if (!TextUtils.isEmpty(accountId)) {
                list.add(accountId);
            }
        }
        return list;
    }
}
